import './searchProject.scss';
import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';
import Search from 'vux/src/components/search/index.vue'
import XProgress from 'vux/src/components/x-progress/index.vue'
import Tab from 'vux/src/components/tab/tab.vue'
import TabItem from 'vux/src/components/tab/tab-item.vue'
import Sticky from 'vux/src/components/sticky/index.vue'
import Flexbox from 'vux/src/components/flexbox/flexbox.vue'
import FlexboxItem from 'vux/src/components/flexbox/flexbox-item.vue'
import Scroller from 'vux/src/components/scroller/index.vue'
export default {
  name: 'searchProject',
  components: {
    Search,
    XProgress,
    Tab,
    TabItem,
    Sticky,
    Flexbox,
    FlexboxItem,
    Scroller},
  data () {
    return {
      inText: '',
      value: '',
      hotList: '',
      typeList: '',

      medicineNum: '',
      otherNum: '',
      poolNum: '',
      studyNum: '',
      oneToOneNum: '',
      allNum: '',

      allnumber: '',
      index: 0,
      isA: true,
      isB: false,
      eventListQB: '',
      eventListjinqi: '',
      eventListwangqi: '',
      eventListsearch: '',
      pageNosearch: 1,
      pageNoJinqi: 1,
      pageNoWangqi: 0,
      pageSize: 6,
      demo: '近期项目',
      noMoreEvent: '点击加载更多',
      use_pullup: true,
      pullup_con: {
        content: ' ',
        pullUpHeight: 60,
        height: 40,
        autoRefresh: false,
        downContent: ' ',
        upContent: ' ',
        loadingContent: ' ',
        clsPrefix: 'xs-plugin-pullup-'
      }
    }
  },
  mounted () {
    this.$refs.search.setFocus()
    http.post(CONSTANT.TOTAL_MONEY, {}, (res) => {
      console.log(res)
      if (res.code == 200) {
        this.typeList = res.obj
        this.allNum = res.obj.whole
        for (let i = 0; i < res.obj.typeNumber.length; i++) {
          if (res.obj.typeNumber[i].eventType == 'medicine') {
            this.medicineNum = res.obj.typeNumber[i].number
          } else if (res.obj.typeNumber[i].eventType == 'other') {
            this.otherNum = res.obj.typeNumber[i].number
          } else if (res.obj.typeNumber[i].eventType == 'pool') {
            this.poolNum = res.obj.typeNumber[i].number
          } else if (res.obj.typeNumber[i].eventType == 'study') {
            this.studyNum = res.obj.typeNumber[i].number
          } else if (res.obj.typeNumber[i].eventType == 'biunique') {
            this.oneToOneNum = res.obj.typeNumber[i].number
          }
        }
      }
    })
    http.post(CONSTANT.EVENT_RECOMMEDN, {}, (res) => {
//        推荐项目请求
      if (res.code == 200) {
        console.log(res)
        this.hotList = res.list
      }
    })
    if (this.$route.query.gjz != '') {
      this.value = this.$route.query.gjz
    }
    if (this.$route.query.type != '') {
      this.jinqiEvent(this.$route.query.type)
      // this.wangqiEvent(this.$route.query.type)
    }
  },
  methods: {
    goSearchPage (e) {
      this.$router.push({path: '/searchProject', query: {type: e}})
      this.allnumber = ''
      this.pageNosearch = 1
      this.pageNoJinqi = 1
      this.pageNoWangqi = 0
      if (this.$route.query.type != '') {
        this.jinqiEvent(this.$route.query.type)
        // this.wangqiEvent(this.$route.query.type)
      }
    },
    goProject (e) {
      window.sessionStorage.setItem('event_id', e)
      this.$router.push({path: '/Project'})
    },
    firstTab () {
      this.isA = true
      this.isB = false
      this.eventListQB = this.eventListjinqi
    },
    secondTab () {
      this.isA = false
      this.isB = true
      if (this.pageNoWangqi == 0) {
        this.wangqiEvent(this.$route.query.type)
      } else {
        this.eventListQB = this.eventListwangqi
      }
    },
    wangqiEvent (e) {
      if (e == 'all') {
        e = ''
      }
      this.pageNoWangqi++
      http.post(CONSTANT.EVENT_PAST, {
        event_type: e,
        pageNo: this.pageNoWangqi,
        pageSize: this.pageSize
      }, (res) => {
        if (res.code == 200) {
          if (res.errmsg == '没有更多的项目') {
            if (this.pageNoWangqi > 1) {
              this.pageNoWangqi--
            }
            return
          }
          if (this.pageNoWangqi == 1) {
            this.eventListwangqi = res.list
          } else {
            this.eventListwangqi = this.eventListwangqi.concat(res.list)
          }
          if (this.isB == true && this.eventListsearch == '') {
            this.eventListQB = this.eventListwangqi
          }
        }
      })
    },
    jinqiEvent (e) {
      if (e == 'all') {
        e = ''
      }
      http.post(CONSTANT.EVENT_LIST, {
        event_type: e,
        pageNo: this.pageNoJinqi,
        pageSize: this.pageSize
      }, (res) => {
        console.log(res)
        if (res.errmsg == '没有更多的项目') {
          if (this.pageNoJinqi > 1) {
            this.pageNoJinqi--
          }
          return
        }
        if (this.pageNoJinqi == 1) {
          console.log(123)
          this.eventListjinqi = res.list
        } else {
          this.eventListjinqi = this.eventListjinqi.concat(res.list)
        }
        if (this.isA == true && this.eventListsearch == '') {
          this.eventListQB = this.eventListjinqi
        }
      })
    },
    onScrollBottom () {
      this.$nextTick(() => {
        this.$refs.scroller.donePullup()
        this.$refs.scroller.reset()
      })
      if (this.eventListjinqi != '' && this.isA == true) {
        this.pageNoJinqi++
        this.jinqiEvent(this.$route.query.type)
      }
      if (this.eventListwangqi != '' && this.isB == true) {
        this.pageNoWangqi++
        this.wangqiEvent(this.$route.query.type)
      }
      if (this.eventListsearch != '') {
        this.pageNosearch++
        http.post(CONSTANT.EVENT_SEARCH, {keyWords: this.value, pageNo: this.pageNosearch, pageSize: this.pageSize}, (res) => {
          console.log(res)
          if (res.code == 200) {
            if (res.errmsg == '没有更多的项目') {
              this.pageNosearch--
              return
            }
            if (this.value != '') {
              this.eventListsearch = this.eventListsearch.concat(res.list)
              this.eventListQB = this.eventListsearch
              this.$nextTick(() => {
                this.$refs.scroller.donePullup()
                this.$refs.scroller.reset()
              })
            }
          }
        })
      }
    },
    setFocus () {
      this.$refs.search.setFocus()
      console.log('setFocus')
    },
    resultClick (item) {
      console.log('resultClick')
    },
    getResult (val) {
      console.log('on-change', val)
      this.pageNosearch = 1
      this.pageNoJinqi = 1
      this.pageNoWangqi = 0
      if (val != '') {
        http.post(CONSTANT.EVENT_SEARCH, {keyWords: val, pageNo: this.pageNosearch, pageSize: this.pageSize}, (res) => {
          console.log(res)
          if (this.value != '') {
            if (res.errmsg == '没有更多的项目') {
              return
            }
            this.allnumber = res.list[0].number
            this.eventListsearch = res.list
            this.eventListQB = this.eventListsearch
            this.eventListjinqi = ''
            this.eventListwangqi = ''
          }
        })
      } else {
        this.eventListQB = ''
        this.eventListjinqi = ''
        this.eventListwangqi = ''
        this.eventListsearch = ''
        this.pageNosearch = 1
        this.allnumber = ''
        this.$nextTick(() => {
          this.$refs.scroller.donePullup()
          this.$refs.scroller.reset()
        })
      }
    },
    onSubmit () {
      this.$refs.search.setBlur()
      if (this.eventListsearch == '') {
        alert('抱歉，未搜索到相关项目！')
      }
    },
    onFocus () {
      console.log('on focus')
    },
    onCancel () {
      window.history.go(-1)
    }
  }
}
function getResult (val) {
  let rs = []
  for (let i = 0; i < 20; i++) {
    rs.push({
      title: `${val} result: ${i + 1} `,
      other: i
    })
  }
  return rs
}
